package com.taplinker.client.crm.rpc;

import android.text.TextUtils;
import android.widget.Toast;

import com.taplinker.client.AppConfigContext;
import com.taplinker.client.crm.domain.CrmDataCenter;
import com.taplinker.client.crm.domain.CustomerInfo;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.rpc.command.SimpleBaseCommand;
import com.taplinker.core.rpc.http.HttpTemplate;
/**
 * 更新用户
 * @author djbian
 *
 */
public class UpdateCustomer extends SimpleBaseCommand {
	private CustomerInfo cus ;
	public UpdateCustomer(CustomerInfo cus) {
		this.cus = cus;
	}

	@Override
	public Result go() {
		String url = "";
		if(TextUtils.isEmpty(cus.getCustomer().getConversationId())) {
			url = "/services/crm/v2/addAndUpdateCustomer";
		} else if ("-1".equals(cus.getCustomer().getConversationId())) {
			url = "/services/im/v2/addCustomer/without/conversation";
		} else {
			url = "/services/im/v2/addAndUpdateCustomer";
		}
		return new HttpTemplate().postForGogoByte(AppConfigContext.TAPLINKER_WEB + url, cus.getCustomer().toByteArray());
	}
	
	@Override
	public void yes(Result result) {
		super.yes(result);
		CrmDataCenter.getInstance().setIds(((String) result.object).split(","));
		CrmDataCenter.getInstance().noticeListener(CrmDataCenter.UPDATE_CUSTOMER);
	}
	
	@Override
	public void no(Result result) {
		Toast.makeText(CrmDataCenter.getInstance().getContext(), "服务器异常", Toast.LENGTH_SHORT).show();
	}

}